package com.learn.sb.redis.reids.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisPiplineDemo {

	@Autowired
	private StringRedisTemplate redisTemplate;

	@Test
	public void testPipLine() {
		Long start = System.currentTimeMillis();
		List list = redisTemplate.executePipelined(new SessionCallback<List>() {
			@Override
			public List execute(RedisOperations redisOperations) throws DataAccessException {
				int num = 100000;
				for (int i = 1; i<= num; ++i) {
					redisOperations.opsForValue().set("pipline" + i, "value" + i);
					String value = String.valueOf(redisOperations.opsForValue().get("pip" + i));
					if (num == i) {
						System.out.println("命令只是进入队列，所以值为空: " + value);
					}
				}
				return null;
			}
		});

		Long end = System.currentTimeMillis();
		System.out.println("耗时：" + (end-start) + "毫秒");
	}


}
